package com.cx.service.impl;

import com.cx.mapper.GoodsMapper;
import com.cx.mapper.SeckillGoodsMapper;
import com.cx.pojo.GoodsWithBLOBs;
import com.cx.pojo.SeckillGoods;
import com.cx.service.SeckillVOService;
import com.cx.vo.SeckillGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author 11597
 * @Date 2020/5/26 11:02
 */
@Service
public class SeckillVOServiceImpl implements SeckillVOService {


    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    RedisUtils redisUtils;


    @Override
    public List<SeckillGoodsVO> selectAllSeckillGoods() {

        boolean hasKey = redisUtils.exists("SeckillGoodsList");
        List<SeckillGoodsVO> seckillGoodsVOList = new ArrayList<>();

        if(hasKey){
            System.out.println("从redis中读取SeckillGoodsList");
            seckillGoodsVOList = (List<SeckillGoodsVO>) redisUtils.get("SeckillGoodsList");
        } else{
            System.out.println("从数据库中查找");
            Date now = new Date();
            List<SeckillGoods> seckillGoodsList = seckillGoodsMapper.selectAllSeckillGoods();
            for(SeckillGoods seckillGoods : seckillGoodsList){
                if(now.after(seckillGoods.getStartTime())&& now.before(seckillGoods.getEndTime())){
                    SeckillGoodsVO seckillGoodsVO = new SeckillGoodsVO();
                    GoodsWithBLOBs goodsWithBLOBs = goodsMapper.selectByPrimaryKey(seckillGoods.getGoodId());
                    seckillGoodsVO.setId(seckillGoods.getId());
                    seckillGoodsVO.setGoodId(seckillGoods.getGoodId());
                    seckillGoodsVO.setGoodNo(goodsWithBLOBs.getGoodNo());
                    seckillGoodsVO.setGoodName(goodsWithBLOBs.getGoodName());
                    seckillGoodsVO.setGoodBrandId(goodsWithBLOBs.getGoodBrandId());
                    seckillGoodsVO.setGoodTypeId(goodsWithBLOBs.getGoodTypeId());
                    seckillGoodsVO.setGoodImages(goodsWithBLOBs.getGoodImages());
                    seckillGoodsVO.setGoodPrice(goodsWithBLOBs.getGoodPrice());
                    seckillGoodsVO.setShopId(goodsWithBLOBs.getShopId());
                    seckillGoodsVO.setGoodDetail(goodsWithBLOBs.getGoodDetail());
                    seckillGoodsVO.setSeckillPrice(seckillGoods.getSeckillPrice());
                    seckillGoodsVO.setSeckillStock(seckillGoods.getSeckillStock());
                    seckillGoodsVO.setSeckillNum(seckillGoods.getSeckillNum());
                    seckillGoodsVO.setStartTime(seckillGoods.getStartTime());
                    seckillGoodsVO.setEndTime(seckillGoods.getEndTime());
                    seckillGoodsVOList.add(seckillGoodsVO);
                }
            }
            redisUtils.set("SeckillGoodsList", seckillGoodsVOList);
        }
        return seckillGoodsVOList;
    }



    @Override
    public List<SeckillGoodsVO> selectAllSeSeckillGoods() {
        List<SeckillGoodsVO> seckillGoodsVOList = new ArrayList<>();

        Date now = new Date();
        List<SeckillGoods> seckillGoodsList = seckillGoodsMapper.selectAllSeckillGoods();
        for(SeckillGoods seckillGoods : seckillGoodsList){
            if(now.after(seckillGoods.getStartTime())&& now.before(seckillGoods.getEndTime())){
                SeckillGoodsVO seckillGoodsVO = new SeckillGoodsVO();
                GoodsWithBLOBs goodsWithBLOBs = goodsMapper.selectByPrimaryKey(seckillGoods.getGoodId());
                seckillGoodsVO.setId(seckillGoods.getId());
                seckillGoodsVO.setGoodId(seckillGoods.getGoodId());
                seckillGoodsVO.setGoodNo(goodsWithBLOBs.getGoodNo());
                seckillGoodsVO.setGoodName(goodsWithBLOBs.getGoodName());
                seckillGoodsVO.setGoodBrandId(goodsWithBLOBs.getGoodBrandId());
                seckillGoodsVO.setGoodTypeId(goodsWithBLOBs.getGoodTypeId());
                seckillGoodsVO.setGoodImages(goodsWithBLOBs.getGoodImages());
                seckillGoodsVO.setGoodPrice(goodsWithBLOBs.getGoodPrice());
                seckillGoodsVO.setShopId(goodsWithBLOBs.getShopId());
                seckillGoodsVO.setGoodDetail(goodsWithBLOBs.getGoodDetail());
                seckillGoodsVO.setSeckillPrice(seckillGoods.getSeckillPrice());
                seckillGoodsVO.setSeckillStock(seckillGoods.getSeckillStock());
                seckillGoodsVO.setSeckillNum(seckillGoods.getSeckillNum());
                seckillGoodsVO.setStartTime(seckillGoods.getStartTime());
                seckillGoodsVO.setEndTime(seckillGoods.getEndTime());
                seckillGoodsVOList.add(seckillGoodsVO);
            }
        }

        return seckillGoodsVOList;
    }


    @Override
    public SeckillGoodsVO selectByIdSeckillGoods(Integer goodId) {
        boolean hasKey = redisUtils.exists(goodId + "");
        SeckillGoodsVO seckillGoodsVO = new SeckillGoodsVO();

        if(hasKey){
            System.out.println("从redis中读取SeckillGoods");
            seckillGoodsVO = (SeckillGoodsVO) redisUtils.get(goodId+"");
        } else{
            System.out.println("从数据库中查找");
            System.out.println("goodId->"+goodId);
            SeckillGoods seckillGoods = seckillGoodsMapper.selectByGoodId(goodId);
            System.out.println("seckillGoods" + seckillGoods);
            GoodsWithBLOBs goodsWithBLOBs = goodsMapper.selectByPrimaryKey(goodId);
            System.out.println("goodsWithBLOBs" + goodsWithBLOBs);
            seckillGoodsVO.setId(seckillGoods.getId());
            seckillGoodsVO.setGoodId(seckillGoods.getGoodId());
            seckillGoodsVO.setGoodNo(goodsWithBLOBs.getGoodNo());
            seckillGoodsVO.setGoodName(goodsWithBLOBs.getGoodName());
            seckillGoodsVO.setGoodBrandId(goodsWithBLOBs.getGoodBrandId());
            seckillGoodsVO.setGoodTypeId(goodsWithBLOBs.getGoodTypeId());
            seckillGoodsVO.setGoodImages(goodsWithBLOBs.getGoodImages());
            seckillGoodsVO.setGoodPrice(goodsWithBLOBs.getGoodPrice());
            seckillGoodsVO.setShopId(goodsWithBLOBs.getShopId());
            seckillGoodsVO.setGoodDetail(goodsWithBLOBs.getGoodDetail());
            seckillGoodsVO.setSeckillPrice(seckillGoods.getSeckillPrice());
            seckillGoodsVO.setSeckillStock(seckillGoods.getSeckillStock());
            seckillGoodsVO.setSeckillNum(seckillGoods.getSeckillNum());
            seckillGoodsVO.setStartTime(seckillGoods.getStartTime());
            seckillGoodsVO.setEndTime(seckillGoods.getEndTime());
            long nowTime = new Date().getTime();
            long endTime = seckillGoodsVO.getEndTime().getTime();
            long  countdown = (endTime - nowTime)/1000;
            redisUtils.set(goodId+"", seckillGoodsVO,countdown,TimeUnit.SECONDS);
        }
        return seckillGoodsVO;
    }




//    @Override
//    public SeckillGoodsVO selectByIdSeSeckillGoods(Integer goodId) {
//        SeckillGoods seckillGoods = seckillGoodsMapper.selectByGoodId(goodId);
//        GoodsWithBLOBs goodsWithBLOBs = goodsMapper.selectByPrimaryKey(goodId);
//        SeckillGoodsVO seckillGoodsVO = new SeckillGoodsVO();
//        seckillGoodsVO.setId(seckillGoods.getId());
//        seckillGoodsVO.setGoodId(seckillGoods.getGoodId());
//        seckillGoodsVO.setGoodNo(goodsWithBLOBs.getGoodNo());
//        seckillGoodsVO.setGoodName(goodsWithBLOBs.getGoodName());
//        seckillGoodsVO.setGoodBrandId(goodsWithBLOBs.getGoodBrandId());
//        seckillGoodsVO.setGoodTypeId(goodsWithBLOBs.getGoodTypeId());
//        seckillGoodsVO.setGoodImages(goodsWithBLOBs.getGoodImages());
//        seckillGoodsVO.setGoodPrice(goodsWithBLOBs.getGoodPrice());
//        seckillGoodsVO.setShopId(goodsWithBLOBs.getShopId());
//        seckillGoodsVO.setGoodDetail(goodsWithBLOBs.getGoodDetail());
//        seckillGoodsVO.setSeckillPrice(seckillGoods.getSeckillPrice());
//        seckillGoodsVO.setSeckillStock(seckillGoods.getSeckillStock());
//        seckillGoodsVO.setSeckillNum(seckillGoods.getSeckillNum());
//        seckillGoodsVO.setStartTime(seckillGoods.getStartTime());
//        seckillGoodsVO.setEndTime(seckillGoods.getEndTime());
//
//        return seckillGoodsVO;
//    }
}
